/*
 * Copyright:  2018 smarabbit studio.
 *
 * Licensed under the Confluent Community License; you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 * http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under
 *  the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 *  either express or implied.  See the License for the specific language governing
 *  permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:    2020/7/11 上午9:09
 *
 */

package com.massyframework.beanskin.spring.beancontext.lazyinit;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.BeanDefinitionDocumentReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

/**
 * {@link LazyInitConfigurerXmlBeanDefinitionReader}, 支持延迟加载设置的Bean定义读取器
 */
public class LazyInitConfigurerXmlBeanDefinitionReader extends XmlBeanDefinitionReader {

    private Boolean defaultLayInit;

    /**
     * 构造方法
     * @param registry {@link BeanDefinitionRegistry}
     */
    public LazyInitConfigurerXmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
        super(registry);
    }

    /**
     * 缺省延迟加载
     * @return {@link Boolean}
     */
    public Boolean getDefaultLayInit() {
        return defaultLayInit;
    }

    /**
     * 设置缺省延迟加载值
     * @param defaultLayInit {@link Boolean}
     */
    public void setDefaultLayInit(Boolean defaultLayInit) {
        this.defaultLayInit = defaultLayInit;
    }

    /**
     * Create the {@link BeanDefinitionDocumentReader} to use for actually
     * reading bean definitions from an XML document.
     * <p>The default implementation instantiates the specified "documentReaderClass".
     *
     * @see #setDocumentReaderClass
     */
    @Override
    protected BeanDefinitionDocumentReader createBeanDefinitionDocumentReader() {
        BeanDefinitionDocumentReader result =  super.createBeanDefinitionDocumentReader();
        if (this.defaultLayInit != null) {
            if (result instanceof LazyInitConfigurerBeanDefinitionDocumentReader) {
                ((LazyInitConfigurerBeanDefinitionDocumentReader) result).setDefaultLazyInit(this.defaultLayInit.toString());
            }
        }

        return result;
    }
}
